/**------------------------------------身份证相关---------------------------------------------*/
/**根据正确的身份证号(支持15位或18位)判断: 
 * 出生日期、性别
 * createtime: 2016-11-24
 */

//身份证号码验证
function validateIdCard(idcard){
	idcard = $.trim(idcard);
	if(!idcard){
		return false;
	}
	var regex1 = /^[1-9][0-7]\d{4}((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))\d{3}(\d|X|x)?$/;

	/* 身份号码位数及格式检验 */
	switch (idcard.length) {
	case 15:
		if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
			var regex2 = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;// 测试出生日期的合法性
		} else {
			var regex2 = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;// 测试出生日期的合法性
		}

		if(regex2.test(idcard)) 
			return true;
		else 
			return false;
		break; 
	case 18:
		if(regex1.test(idcard)){
			var S = (parseInt(idcard.charAt(0)) + parseInt(idcard.charAt(10))) * 7 + 
			(parseInt(idcard.charAt(1)) + parseInt(idcard.charAt(11))) * 9 + 
			(parseInt(idcard.charAt(2)) + parseInt(idcard.charAt(12))) * 10 + 
			(parseInt(idcard.charAt(3)) + parseInt(idcard.charAt(13))) * 5 + 
			(parseInt(idcard.charAt(4)) + parseInt(idcard.charAt(14))) * 8 + 
			(parseInt(idcard.charAt(5)) + parseInt(idcard.charAt(15))) * 4 + 
			(parseInt(idcard.charAt(6)) + parseInt(idcard.charAt(16))) * 2 + 
			parseInt(idcard.charAt(7)) * 1 + 
			parseInt(idcard.charAt(8)) * 6 + 
			parseInt(idcard.charAt(9)) * 3;
			var Y = S % 11;
			var M = "F";
			var JYM = "10X98765432";
			M = JYM.substr(Y, 1);
			/* 判断校验位 */
			if (M == idcard.charAt(17).toUpperCase()) {
				return true;
			} else {
				return false;
			}
		}else{
			return false;
		}
		break;
	default:
		return false;
	}
}
//根据身份证算出出生年月
function getBirthdayFromIdNo(idcard){
	idcard = $.trim(idcard);
	var tmpStr="";
	if(idcard.length == 15){
		tmpStr=idcard.substring(6,12);
		tmpStr= "19" + tmpStr;
		tmpStr= tmpStr.substring(0,4) + "-" + tmpStr.substring(4,6) + "-" + tmpStr.substring(6);
	}else if(idcard.length == 18){
		tmpStr=idcard.substring(6,14);
		tmpStr= tmpStr.substring(0,4) + "-" + tmpStr.substring(4,6) + "-" + tmpStr.substring(6);
	}else {
		return "";  
	}
	return tmpStr;
}

/**
 * 根据身份证号判断性别
 * 人身险核心系统性别码表: 1-男 2-女 3-未知的性别 9-未说明的性别
 * */ 
function getSex(idcard){
	idcard = $.trim(idcard);
	var sex = 3;
	if(idcard.length == 18){
		sex = parseInt(idcard.substr(16, 1)) % 2 == 1?1:2;
	}else if(idcard.length == 15){
		sex = parseInt(idcard.substr(14)) % 2 == 1?1:2;
	}
	return sex;
}
/**------------------------------------身份证相关---------------------------------------------*/